<template>
	<view class="web-box">
		<web-view class="webview" :src="url" :update-title="$uv.test.empty(props.title)"></web-view>
	</view>
</template>

<script setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { ref } from 'vue'
	const url = ref('')
	const props = defineProps(['url', 'title'])
	url.value = props.url
	
	// 处理webview适配移动端，给h5添加移动端适配meta
	const handleWebViewAdaptiveMobile = ()=>{
		const pages = getCurrentPages()
		const page = pages[pages.length - 1]
		// #ifdef APP-PLUS  
		//页面栈最顶层就是当前webview  
		const currentWebview = page.$getAppWebview()
		setTimeout(() => {
			const subWebView = currentWebview.children()[0]
			const js = `var meta = document.createElement('meta');
						meta.name = 'viewport';
						meta.content = 'width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0';
						document.getElementsByTagName('head')[0].appendChild(meta);`
			subWebView.evalJS(js)
		}, 100)
		// #endif
	}

	onLoad(() => {
		if (props.title) {
			uni.setNavigationBarTitle({
				title: props.title
			})
			handleWebViewAdaptiveMobile()
		}
	})
</script>
<style>
</style>